package com.yc.pro2;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;
import java.util.List;

public class Test2 {
    public static void main( String[] args )
    {//初始化 流控规则
        initFlowRules();

        //模拟流量很大的情况(因为cpu运行速度快,所以调用次数会非常高)
        while (true){
            try(Entry entry = SphU.entry("HelloWorld")){
                System.out.println("Hello World!");
            }catch (BlockException e){
                System.out.println("限流了");
            }
        }
    }
    @SentinelResource("helloWorld")
    public static void helloWorld(){
        System.out.println("hello world");
    }


    private static void initFlowRules(){
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("HelloWorld");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setCount(200000);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}